<template>
	<view>
		<u-input
		  class="underline-input"
		  placeholder="请输入新邮箱"
		  type="text"
		  v-model="newEmail"
		  clearable
		  autoBlur
		 
		></u-input>
		<u-input
		  class="underline-input"
		  placeholder="请输入验证码"
		  type="text"
		  v-model="verificationCode"
		  clearable
		  autoBlur
		></u-input>
		<u-button @click="getVerificationCode()" text="发送验证码"></u-button>
		<u-button @click="update()" text="确认"></u-button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				newEmail:'',
				verificationCode:'',
				uid:'',
				token:'',
			}
		},
		methods: {
			initPage(){
				this.uid=uni.getStorageSync('UID');
				this.token=uni.getStorageSync('token');
				console.log(this.uid);
				if(this.uid!=''&&this.token!=''){
				//this.getEmail();
				//this.getVerificationCode();
				//this.update();
				}
				
			},
			update(){
				uni.request({
					url:this.baseUrl+'/setEmail',
					method:'POST',
					data:{
						data:{
							email:this.newEmail,
							userId:this.uid,
							
						},
						msgType:'setEmail'
					},
					header:{
						'Content-Type': 'application/json' ,// 如果需要以JSON格式发送数据
						uid: this.uid, // 用户唯一标识
						token: this.token // 身份认证 Token
					},
					success: (res) => {
						uni.hideLoading();
						uni.showToast({
						  title: res.data.data,
						  icon: 'success',
						  duration: 2000
						});
					
					}
					
				})
				
			},
			  validateEmail(email) {
			    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
			    return emailRegex.test(email);
			  },
			  getVerificationCode() {
				  if (!this.validateEmail(this.newEmail)) {
				    uni.showToast({
				      title: '邮箱格式不正确',
				      icon: 'error',
				      duration: 2000
				    });
				    return;
				  }
				  console.log('uid:',this.uid);
		       console.log('注册邮箱:', this.newEmail, '获取验证码');
		       uni.showLoading({
		         title: 'Loading',
		         mask: true
		       });
		       uni.request({
		         url: this.baseUrl + '/getEmailVerifyCode',
		         method: 'POST',
		         data: {
		           data: {
		             email: this.newEmail,
		           },
		           msgType: 'getEmailVerifyCode'
		         },
		         header: {
		           'Content-Type': 'application/json',
				   uid: this.uid, // 用户唯一标识
				   token: this.token // 身份认证 Token
		         },
		         success: (res) => {
		           uni.hideLoading();
		           if (res.data.code == 1) {
		             uni.showToast({
		               title: res.data.data,
		               icon: 'success',
		               duration: 2000
		             });
					 console.log(res.data.data);
                     this.update();
		           } else {
		             uni.showToast({
		               title: res.data.msg,
		               icon: 'none',
		               duration: 2000
		             });
		           }
		         },
		         fail: (err) => {
		           uni.hideLoading();
		           uni.showToast({
		             title: err,
		             icon: 'none',
		             duration: 2000
		           });
		         }
		       });
		     },	
		},
		mounted() {
			this.initPage()
		}
	}
</script>

<style>

</style>
